package com.android.hw.projectmenu;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.os.BatteryManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.PowerManager;
import android.util.Log;
import android.widget.TextView;

public class ChargingMenu extends Activity {
	
	private int mCurBatteryLevel = 0;
	private BroadcastReceiver mBroadcastReceiver;
	private TextView mCurBatteryLevelTv;
	private TextView mCurBatteryTempTv;
	private TextView mCurBatteryStateTv;
	private String TAG = "ChargingMenu";
	PowerManager.WakeLock mWl;
	
	private static final int MSG_GET_BATTERY_TEMP = 0;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.charging_menu);
		mCurBatteryLevelTv = (TextView) findViewById(R.id.batterylevel_left_value);
		mCurBatteryTempTv = (TextView) findViewById(R.id.battery_temp_value);
		mCurBatteryStateTv = (TextView) findViewById(R.id.charging_state);
		PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
		mWl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "ChargingMenu"); 
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		mHandler.sendEmptyMessage(MSG_GET_BATTERY_TEMP);
		mBroadcastReceiver = new BatteryBroadcastReciver();
		IntentFilter intentFilter = new IntentFilter(
				Intent.ACTION_BATTERY_CHANGED);
		registerReceiver(mBroadcastReceiver, intentFilter);
		mWl.acquire();
	}
	
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		unregisterReceiver(mBroadcastReceiver);
		mHandler.removeMessages(MSG_GET_BATTERY_TEMP);
		mWl.release();
	}
	
	private Handler mHandler =new Handler(){
		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			float temp = Float.valueOf(getBatteryTemp());
			temp = temp / 10;
			if (temp <= 70) {
				mCurBatteryTempTv.setTextColor(Color.GREEN);
			} else {
				mCurBatteryTempTv.setTextColor(Color.RED);
			}
			mCurBatteryTempTv.setText(String.valueOf(temp) + "°C");
			mHandler.sendEmptyMessageDelayed(MSG_GET_BATTERY_TEMP, 1000);
		}
	};
	
	public String getBatteryTemp() {
		try {
			FileInputStream inStream = new FileInputStream(
					"/sys/class/power_supply/bms/temp");
			byte[] buffer = new byte[8];
			int nu = inStream.read(buffer);
			inStream.close();
			Log.e(TAG, "nu=" + nu);
			Log.e(TAG, "buffer.toString()=" + new String(buffer));
			return new String(buffer);

		} catch (FileNotFoundException e) {
			e.printStackTrace();
			// default test mode without usb_mode file.
			return "-1000";
		} catch (IOException e) {
			e.printStackTrace();
			return "-1000";
		}
	}
	
	class BatteryBroadcastReciver extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			// TODO Auto-generated method stub
			if (intent.getAction().equals(Intent.ACTION_BATTERY_CHANGED)) {
				int level = intent.getIntExtra("level", 0);
				int total = intent.getIntExtra("scale", 100);
				mCurBatteryLevel = (level * 100) / total;
				mCurBatteryLevelTv.setText(mCurBatteryLevel + "%");
				if (mCurBatteryLevel >= 55 && mCurBatteryLevel <= 70) {
					mCurBatteryLevelTv.setTextColor(Color.GREEN);
				} else {
					mCurBatteryLevelTv.setTextColor(Color.RED);
				}
				
				int status = intent.getIntExtra("status",
						BatteryManager.BATTERY_STATUS_UNKNOWN);
				Log.d(TAG, "status="+status);
				if (status == BatteryManager.BATTERY_STATUS_CHARGING) {
					if (mCurBatteryLevel <= 55) {
						mCurBatteryStateTv.setText(R.string.charging_now);
						mCurBatteryStateTv.setTextColor(Color.RED);
					} else {
						mCurBatteryStateTv.setText(R.string.charging_complete);
						mCurBatteryStateTv.setTextColor(Color.GREEN);
					}
				} else {
					if (mCurBatteryLevel > 55) {
						mCurBatteryStateTv.setText(R.string.charging_complete);
						mCurBatteryStateTv.setTextColor(Color.GREEN);
					} else {
						mCurBatteryStateTv.setText(R.string.not_charging);
						mCurBatteryStateTv.setTextColor(Color.RED);
					}
				}
			}
		}

	}
}
